package com.thti.mecool.client.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.model.biz.TaskStepBizImpl;
import com.thti.mecool.client.model.po.TaskStep;
import com.thti.mecool.client.widget.CheckView;
import com.thti.mecool.client.widget.Toolbar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskStepScoreActivity extends BaseActivity{
	private TaskStepBizImpl taskStepBizImpl;
	private String planId,parentId,projectId,taskId;
	private ArrayList<TaskStep> taskSteps;
	private SparseBooleanArray sArray;
	private SparseArray<SparseArray<String>> choiceArray;
	private TextView action_text,action_stat;
	private SparseBooleanArray isFinishs;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	noTitle();
	setContentView(R.layout.mecool_task_action);
	new Toolbar(this);
	Toolbar.left_btn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
		}
	});
	Intent i=getIntent();
	planId=i.getStringExtra("planId");
	parentId=i.getStringExtra("parentId");
	projectId=i.getStringExtra("projectId");
    taskId = i.getStringExtra("taskId");
    isFinishs=new SparseBooleanArray();
	action_text = (TextView) findViewById(R.id.mecool_action_text);
	action_stat = (TextView) findViewById(R.id.mecool_action_stat);
	action_stat.setVisibility(8);
	action_text.setText(i.getStringExtra("text_name"));
	sArray=new SparseBooleanArray();
	taskSteps=new ArrayList<TaskStep>();
	taskStepBizImpl=new TaskStepBizImpl(this);
	choiceArray=new SparseArray<SparseArray<String>>();
	taskSteps.addAll(taskStepBizImpl.findByStep(planId, projectId, parentId,taskId));
	for(TaskStep ts:taskSteps){
		boolean flag=false;
		SparseArray<String> choice=new SparseArray<String>();
		List<TaskStep> tss=taskStepBizImpl.findBelowStep(planId, projectId, ts.getStepId(), taskId);
		for(TaskStep ts2:tss){
			choice.put(Integer.valueOf(ts2.getExtra2()), ts2.getStepId());
			if(Integer.valueOf(ts2.getExtra2())==1){
				flag=ts2.getIsFinsh()==1?true:false;
			}
		}
		isFinishs.put(Integer.valueOf(ts.getStepId()), flag);
		choiceArray.put(Integer.valueOf(ts.getStepId()), choice);
	}
	
	System.out.println("taskSteps  size      :      "+taskSteps);
	ListView lv=(ListView)findViewById(R.id.mecool_action_list);
	lv.setAdapter(ba);
	lv.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			CheckView mBox=(CheckView)arg1;
			mBox.toggle();
			System.out.println("((CheckView)convertView).isChecked()   "+mBox.isChecked());
			if(mBox.isChecked()){
				List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();
				Map<String, Object[]> map = new HashMap<String, Object[]>();
				String[] ids = new String[] { "1", planId, projectId,
						choiceArray.get(Integer.valueOf(taskSteps.get(arg2).getStepId())).get(1), taskId };
				map.put(HandlerConstant.PLAN_UPDATE_SQL, ids);
				

				Map<String, Object[]> map2 = new HashMap<String, Object[]>();
				String[] ids2 = new String[]{"0", planId, projectId,
						choiceArray.get(Integer.valueOf(taskSteps.get(arg2).getStepId())).get(0), taskId};
				map2.put(HandlerConstant.PLAN_UPDATE_SQL, ids2);

				list.add(map2);
				list.add(map);
				taskStepBizImpl.updateStep(list);
				sArray.put(arg2, true);
				}
			else{
				List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();
				Map<String, Object[]> map = new HashMap<String, Object[]>();
				String[] ids = new String[]{"0", planId, projectId,
						choiceArray.get(Integer.valueOf(taskSteps.get(arg2).getStepId())).get(1), taskId};
				map.put(HandlerConstant.PLAN_UPDATE_SQL, ids);

				Map<String, Object[]> map2 = new HashMap<String, Object[]>();
				
				String[] ids2 = new String[]{"1", planId, projectId,
						choiceArray.get(Integer.valueOf(taskSteps.get(arg2).getStepId())).get(0), taskId};
				map2.put(HandlerConstant.PLAN_UPDATE_SQL, ids2);

				list.add(map2);
				list.add(map);
				taskStepBizImpl.updateStep(list);
				sArray.delete(arg2);
				}

			if(sArray.size()>0){
				List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();
				Map<String, Object[]> map = new HashMap<String, Object[]>();
				String[] ids = new String[] { "1", planId, projectId,
						parentId, taskId };
				map.put(HandlerConstant.PLAN_UPDATE_SQL, ids);
				list.add(map);
				taskStepBizImpl.updateStep(list);
			}else{
					List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();
					Map<String, Object[]> map = new HashMap<String, Object[]>();
					String[] ids = new String[] { "0", planId, projectId,
							parentId, taskId };
					map.put(HandlerConstant.PLAN_UPDATE_SQL, ids);
					list.add(map);
					taskStepBizImpl.updateStep(list);
			}
		}
	});
}

BaseAdapter ba=new BaseAdapter() {
	class ViewHolder{
		View view;
		TextView name,score;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if(convertView==null)
		{
			viewHolder=new ViewHolder();
			viewHolder.view=getLayoutInflater().inflate(R.layout.mecool_task_step_score_item, null);
			viewHolder.name=(TextView) viewHolder.view.findViewById(R.id.mecool_task_step_score_item_name);
			viewHolder.name.setWidth(getWindowManager().getDefaultDisplay().getWidth()/5*3);
			viewHolder.score=(TextView) viewHolder.view.findViewById(R.id.mecool_task_step_score_item_score);
			viewHolder.score.setPadding(getWindowManager().getDefaultDisplay().getWidth()/36*3, 0, getWindowManager().getDefaultDisplay().getWidth()/36*3,0);
			convertView=new CheckView(TaskStepScoreActivity.this).setView(viewHolder.view).initCheck();
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.name.setText(taskSteps.get(position).getName());
		viewHolder.score.setText(taskSteps.get(position).getExtra2());
		if(isFinishs.get(Integer.valueOf(taskSteps.get(position).getStepId()))){
			System.out.println("truetruetruetruetruetruetruetruetruetruetrue");
			((CheckView)convertView).toggle();
		}
		return convertView;
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	} 
	
	@Override
	public Object getItem(int position) {
		return taskSteps.get(position);
	}
	
	@Override
	public int getCount() {
		return taskSteps.size();
	}
};
}
